package com.lanou.dllo.findfood.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.findfood.activity.HomePagelastActivity;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.util.MyImageClick;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/21.
 */
public class HomeTopFragment extends Fragment implements View.OnClickListener {
    private String title;
    private int position, deal_id;
    private String image;
    private ImageView adIv;
    private String dealses;
    private String imageUrl;
    private TextView titleTv, dealse;
    private MyImageClick imageClick;


    //单例模式
    private static HomeTopFragment homeTopFragment;

    private HomeTopFragment() {

    }

    public void setImageListener(MyImageClick listener) {

        imageClick = listener;
    }

    //单例模式
    public static HomeTopFragment getInstrance(String title, String image, int position
            , int deal_id, String dealses) {

        HomeTopFragment homeTopFragment = new HomeTopFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("image", image);
        args.putInt("position", position);
        args.putString("dealses", dealses);
        args.putInt("deal_id", deal_id);

        homeTopFragment.setArguments(args);
        return homeTopFragment;

    }

    //初始化队列

    public void onAttach(Context context) {
        super.onAttach(context);


    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            title = bundle.getString("title");
            deal_id = bundle.getInt("deal_id");
            image = bundle.getString("image");
            position = bundle.getInt("position");
            dealses = bundle.getString("dealses");
        }
    }


    //设置图片
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_top, null);
        adIv = (ImageView) view.findViewById(R.id.adIv);
        dealse = (TextView) view.findViewById(R.id.textpage);
        dealse.setText(dealses);
        titleTv = (TextView) view.findViewById(R.id.texTv);
        titleTv.setText(title);//添加文字
        getBitmap(imageUrl, adIv);//图片地址
        adIv.setOnClickListener(this);
        return view;
    }


    public void getBitmap(String imageUrl, final ImageView imageView) {
        ImageRequest request = new ImageRequest(image, new Response.Listener<Bitmap>() {
            private Bitmap response;

            @Override
            public void onResponse(Bitmap response) {

                imageView.setImageBitmap(response);//返回的图片添加进去

            }
        }, 720, 350, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;

            }
        };
        VolleySington.getInstance().addRequest(request);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.adIv:
                Intent intent = new Intent(getActivity(), HomePagelastActivity.class);
                intent.putExtra("deal_id", deal_id);
                startActivity(intent);
                break;
            default:
                break;

        }
    }
}
